# _*_ coding:utf-8 _*_
# @Time : 2021/9/17 17:49
# @Author : wsl
# @File : Urllib
# @Project : python  百度翻译的详细翻译  第二次反扒：Cookie

import urllib.request
import urllib.parse

base_url = 'https://fanyi.baidu.com/v2transapi?from=en&to=zh'

headers = {
    # 'Accept': '*/*',
    # 'Accept-Encoding': 'gzip, deflate, br',
    # 'Accept-Language': 'zh-CN,zh;q=0.9',
    # 'Acs-Token': '1685003516235_1685003531502_jws5fX9ZD42c/ZZ6Ffa3nZGVtTRcplUCyfdhWmeKGphj2LBhYNXa8CmZctUWlv9o5lb/TF4LkAIZ+DvSfEVilXqmi5tFtyiWWUzB2wwbUarvT3xTH++SX3oNtEV6Ku6qp6spJ0CPL7p7+vUVg04wnA67ZnrS866pbrfwWaTUYn5YWaKnsg67rtTVDs7SAQF2kzxaHHiO8xAMSgoaP40YveA1fDViuk8rxLmqzATqjJGbqf2XW6Bb+gb+TKOycq/jVAhCPyx9xObi1+UXabyMaxicWziNDxCsrt/oPCh8tLZo1/ZB9Kv8PkLXNc4XmP0Wo4hxGOOhQs0LZletw63/iLbFus5fxxLKVDDbKcGM9AsnYEIiu69KeZm1fgQf22r+5KfzgthaAMM8Ycbmo9r9Zbkdrv3ckv8cAjWzC/ZLILgQ+f7nGYzmMwzqUKppTmuL6mApO0/duPHpLd400U8AdlMtRMFUKW6jLBLuWNO05XD5e7+u6dVsVEFdLA2mtU6dWq3wkINf3vgJM+JklFssoA==',
    # 'Cache-Control': 'no-cache',
    # 'Connection': 'keep-alive',
    # 'Content-Length': '152',
    # 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Cookie': 'BIDUPSID=E0B05F5DA883F1CE530B0A07F740DAAC; PSTM=1627019121; __yjs_duid=1_ee462671a13aea34dc92a2e0da8be4751628506522571; REALTIME_TRANS_SWITCH=1; HISTORY_SWITCH=1; FANYI_WORD_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BAIDUID=DCE8E680EE591EAC6438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HdWBpbht57jc7Jv-p_fT7afE5XiGa4l3G2s-37DDb44K_r82VEbqUuloizSjSr9DwrTWPwJcy-77WxmTWiB9QePNo3XK3O2nr64tQHV84uz-oSUqzaZwyqfRNWpTx9DN-9zobBoInHWvYvtzHx_O4GdvI4n_VDDnD2L81uwpzB9a4A01NJKOtD0JPITwc_IopwyXC9to_Sz3wFfZ7nDv3ltB_qcX8D6GEnfSFFmpbl0ZDLmHJTzvtLgETVYEVCX0lTBBqhiw8NV5oEqPebbPDDFg3Q0PjiopwkjNNWJgmB4AvgzEi25vXzJ3-8NCRnx7xJmwEg46g1r4o8TYv0XbpUwgBlCA5_S62PUCreFoI1OspfegRZzWq_IWF8zu579FugP-nnzEFyXiSrIuQtne-Uk; H_WISE_SIDS=219946_131861_132548_219558_216837_213355_214799_219942_213028_204907_110085_236312_243873_244724_240590_245411_250137_247148_249893_251786_253427_253213_255937_255982_107315_256083_255659_256349_254831_256739_251973_254317_256940_257081_254075_257292_257660_257738_257936_257824_257586_255224_258248_258166_257994_258471_258370_258376_258724_258729_258756_258837_258938_258984_258958_230288_259048_259037_257015_259190_259193_256223_259405_259285_259391_259430_259568_259605_259625_256999_259558_259656_254469_259704_258772_234295_234208_259883_259889_259642_255909_259052_260034_254300_260123_257302_259240_256229_259721_260365_260225_259187_253022_255212_260370_260582_258081_260331_260804_260834_259408_250303_259422_259584_260976_261003_259306_261029_188332_259113_261299_261374_253900_261343_259448; H_WISE_SIDS_BFESS=219946_131861_132548_219558_216837_213355_214799_219942_213028_204907_110085_236312_243873_244724_240590_245411_250137_247148_249893_251786_253427_253213_255937_255982_107315_256083_255659_256349_254831_256739_251973_254317_256940_257081_254075_257292_257660_257738_257936_257824_257586_255224_258248_258166_257994_258471_258370_258376_258724_258729_258756_258837_258938_258984_258958_230288_259048_259037_257015_259190_259193_256223_259405_259285_259391_259430_259568_259605_259625_256999_259558_259656_254469_259704_258772_234295_234208_259883_259889_259642_255909_259052_260034_254300_260123_257302_259240_256229_259721_260365_260225_259187_253022_255212_260370_260582_258081_260331_260804_260834_259408_250303_259422_259584_260976_261003_259306_261029_188332_259113_261299_261374_253900_261343_259448; SE_LAUNCH=5%3A1684998827; PSINO=5; H_PS_PSSID=38516_36543_38673_38617_38591_38595_38375_38639_26350_22160; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1683355490,1683788644,1685000899; Hm_lvt_afd111fa62852d1f37001d1f980b6800=1685000913; Hm_lpvt_afd111fa62852d1f37001d1f980b6800=1685003495; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1685003516; ab_sr=1.0.1_NzhlNWQ1NDhhNDdjOTAxOGY3MWZhYTY1YTVkNGQxY2Q3NjY5YjVjNWJlZjQ4OGY0OWFiYWM4MjcxOWNkNjc3MTdlYzIxOWYxYjA2MzdjMWI4NmQ1ZTA0ZDMxNWYzM2ZiOWQxZWEwODY1NzEwNGIzNjcyY2E5YjQzYjY1N2UyNTExN2EyOWQxMWQyYTliMzU3M2U2N2M5ZGMyMTkyZDMwOGRmNjhhNWFjOWIzMGUyZjhhNjUzYmE4OTEyOWRhMWE2',
    # 'Host': 'fanyi.baidu.com',
    # 'Origin': 'https://fanyi.baidu.com',
    # 'Pragma': 'no-cache',
    # 'Referer': 'https://fanyi.baidu.com/?aldtype=16047',
    # 'Sec-Ch-Ua': '"Google Chrome";v="113", "Chromium";v="113", "Not-A.Brand";v="24"',
    # 'Sec-Ch-Ua-Mobile': '?1',
    # 'Sec-Ch-Ua-Platform': '"Android"',
    # 'Sec-Fetch-Dest': 'empty',
    # 'Sec-Fetch-Mode': 'cors',
    # 'Sec-Fetch-Site': 'same-origin',
    # 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0',
    # 'X-Requested-With': 'XMLHttpRequest',
}


data = {
    "from": "en",
    "to": "zh",
    "query": "love",
    "transtype": "realtime",
    "simple_means_flag": "3",
    "sign": "198772.518981",
    "token": "a32f29a3ffb0fa65640c1ee88263ec5e",
    "domain": "common"
}

# post请求的参数 要编码 编码后调用encode 不拼接到url 要在请求对象的定制参数中
new_data = urllib.parse.urlencode(data).encode('utf-8')
request = urllib.request.Request(url=base_url, headers=headers, data=new_data)
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
print(content)
# string -> test.json

import json

obj = json.loads(content)
print(obj)
